﻿using B.MESUI.Login.BasicData_UI.BasicData;
using B.MESUI.Login.BasicData_UI.BasicData.Select;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using Standard;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace B.MESUI.Login.BasicData_UI.WorkStation
{
    
    public partial class UpdateStationForm : DevExpress.XtraEditors.XtraForm
    {
        public delegate void UpDate_Load();
        public  event UpDate_Load updateload;
        // 用于保存产线名称和ID的映射
        private Dictionary<string, string> lineNameIdMap = new Dictionary<string, string>();
        public UpdateStationForm(GetStationDTO getStationDTO)
        {

            InitializeComponent();
            // 根据Id回显
             textEdit3.Text= getStationDTO.id;
            textEdit1.Text=getStationDTO.workstatioN_CODE;
           textEdit2.Text=  getStationDTO.workstatioN_NAME ;
            gridLookUpEdit1.EditValue=getStationDTO.linE_ID;
            textEdit4.Text=getStationDTO.workstatioN_DESCRIPTION ;

            
        }
        private async void button1_Click(object sender, EventArgs e)
        {

            UpdateStationDTO updateStationDTO = new UpdateStationDTO()
            {
              id=textEdit3.Text,
                workstatioN_CODE = textEdit1.Text,
                workstatioN_NAME = textEdit2.Text,
                linE_ID = lineNameIdMap.ContainsKey(gridLookUpEdit1.Text) ? lineNameIdMap[gridLookUpEdit1.Text] : null,
                workstatioN_DESCRIPTION = textEdit4.Text
            };
          
            using (HttpClient client = new HttpClient())
            {
              
                var data= Newtonsoft.Json.JsonConvert.SerializeObject(updateStationDTO);
                // 创建一个StringContent对象，指定编码和媒体类型
                var content = new StringContent(data, Encoding.UTF8, "application/json");
                string url = $"http://39.103.49.30:9982/api/WorkStation/UpdateWorkStation";
                var reposense = await client.PutAsync(url, content);
                if (reposense.IsSuccessStatusCode)
                {
                    var json = await reposense.Content.ReadAsStringAsync();
                    dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
                    if (result.code == "200")
                    {
                        MessageBox.Show("修改成功");
                        this.Close();
                        updateload.Invoke();
                    }
                    else
                    {
                        MessageBox.Show("修改失败");
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textEdit1.Text = "";
            textEdit2.Text = "";
            textEdit3.Text = "";
            gridLookUpEdit1.Text = "";
            textEdit4.Text = "";
        }

        private void UpdateStationForm_Load(object sender, EventArgs e)
        {
            string url = $"http://39.103.49.30:9982/api/ProjectLine/GetProductionLine";
            using (HttpClient client = new HttpClient())
            {
                var response = client.GetAsync(url).Result;
                if (response.IsSuccessStatusCode)
                {
                    var respStr = response.Content.ReadAsStringAsync().Result;
                    var result = Newtonsoft.Json.JsonConvert.DeserializeObject<ApiResult<List<SelectLineDTO>>>(respStr);
                    if (result != null && result.ResultData != null)
                    {
                        // 清空之前的映射
                        lineNameIdMap.Clear();
                        // 遍历结果并填充映射
                        foreach (var item in result.ResultData)
                        {
                            lineNameIdMap[item.LINE_NAME] = item.ID;
                        }
                        // 设置GridLookUpEdit的数据源
                        gridLookUpEdit1.Properties.DataSource = result.ResultData;
                        gridLookUpEdit1.Properties.DisplayMember = "LINE_NAME";
                        gridLookUpEdit1.Properties.ValueMember = "ID";
                    }
                }
            }
        }
    }
}